package com.work.service.transport;

import com.work.constant.DictBizConstant;
import com.work.constant.ExceptionDescription;
import com.work.dto.main.TransportCorpSearchDTO;
import com.work.dto.transport.TransportSearchDTO;
import com.work.dto.transport.WorkArrangeSearchDTO;
import com.work.mapper.transport.TransportMapper;
import com.work.vo.transport.*;
import lombok.AllArgsConstructor;
import org.codehaus.jackson.map.ser.std.ObjectArraySerializer;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 货运接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/2/20 13:50
 */
@Service
@AllArgsConstructor
public class TransportServiceImpl implements ITransportService {

    TransportMapper transportMapper;

    @Override
    public TransportBillVO selectTransportBillDetail(TransportSearchDTO dto) {
        if (Func.isNull(dto.getTransportBillId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "舱单ID为空");
        }
        TransportBillVO res =new TransportBillVO();
        List<TransportBillVO> list = transportMapper.selectTransportBillList(dto);
        if (!Func.isNull(list) && !Func.isEmpty(list)) {
            res = list.get(0);
        }
        return res;
    }

    @Override
    public List<TransportBillVO> selectTransportBillList(TransportSearchDTO dto) {
        if (Func.isNull(dto.getTransportShipId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "船ID为空");
        }
        return transportMapper.selectTransportBillList(dto);
    }

    @Override
    public TransportShipVO selectTransportShipDetail(TransportSearchDTO dto) {
        if (Func.isNull(dto.getTransportShipId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "船ID为空");
        }

        TransportShipVO res =new TransportShipVO();
        List<TransportShipVO> list = transportMapper.selectTransportShipList(dto);
        if (!Func.isNull(list) && !Func.isEmpty(list)) {
            res = list.get(0);
        }
        return res;
    }

    @Override
    public List<TransportShipVO> selectTransportShipList(TransportSearchDTO dto) {
        if (Func.isEmpty(dto.getKeyword())) {
            dto.setIsDefault(1);
        }
        return transportMapper.selectTransportShipList(dto);
    }

    @Override
    public TransportShipVO selectTransportShip(TransportSearchDTO dto) {
        if (Func.isEmpty(dto.getTransportShipId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "船舶预报ID为空");
        }
        return transportMapper.selectTransportShipList(dto).get(0);
    }

    @Override
    public String getPackageTypeNameByCode(String packageTypeCode) {
        return transportMapper.selectTransportPackageTypeCode(packageTypeCode);
    }

    @Override
    public List<TransportPackageTypeVO> selectTransportPackageTypeList(TransportSearchDTO dto) {
        return transportMapper.selectTransportPackageTypeList(dto);
    }

    @Override
    public List<CargoTypeVO> getShipCargoType(TransportSearchDTO dto) {
        return transportMapper.selectShipCargoTypeList(dto);
    }

    @Override
    public List<WorkArrangeShipVO> selectWorkArrangeShipList(WorkArrangeSearchDTO dto) {
        List<WorkArrangeShipVO> data = transportMapper.selectWorkArrangeShipList(dto);
        for (WorkArrangeShipVO vo : data) {
            if (!Func.isNull(vo.getHolds()) && Func.isNotBlank(vo.getHolds())) {
                String[] holdsTemp = vo.getHolds().split(",");
                HashSet<String> holdsSet = new HashSet<>();
                Collections.addAll(holdsSet, holdsTemp);
                holdsSet.remove("");

                List<String> holdRes = new ArrayList<>();
                List<String> holdNameRes = new ArrayList<>();
                List<HoldVO> holdVoRes = new ArrayList<>();
                if (!holdsSet.isEmpty()) {
                    TreeSet<String> holdsTreeSet = new TreeSet<>(holdsSet);
                    for (String s : holdsTreeSet) {
                        holdRes.add(s);

                        HoldVO holdVO = new HoldVO();
                        holdVO.setHold(s);

                        String[] splitHold = s.split("_");
                        StringBuilder holdName = new StringBuilder();
                        holdName.append(DictBizCache.getValue(DictBizConstant.HOLD, splitHold[0]));
                        if (!"ALL".equals(splitHold[1])) {
                            holdName.append(DictBizCache.getValue(DictBizConstant.HOLD_POSITION, splitHold[1]));
                        }
                        holdNameRes.add(holdName.toString());

                        holdVO.setHoldName(holdName.toString());

                        holdVoRes.add(holdVO);
                    }
                }
                vo.setHolds(StringUtil.join(holdRes, ","));
                vo.setHoldsName(StringUtil.join(holdNameRes, ","));
                vo.setHoldsList(holdVoRes);
            }
        }
        return data;
    }

    @Override
    public List<WorkArrangeLandVO> selectWorkArrangeLandList(WorkArrangeSearchDTO dto) {
        return transportMapper.selectWorkArrangeLandList(dto);
    }

    @Override
    public List<AssignWorkerVO> selectAssignWorkerList(WorkArrangeSearchDTO dto) {
        return transportMapper.selectAssignWorkerList(dto);
    }

    @Override
    public List<AssignMachineKindVO> selectAssignMachineList(WorkArrangeSearchDTO dto) {
        return transportMapper.selectAssignMachineList(dto);
    }

    @Override
    public List<CorpTeamVO> selectCorpTeamList(TransportCorpSearchDTO dto) {
        return transportMapper.selectCorpTeamList(dto);
    }

    @Override
    public List<CargoTypeVO> selectCargoTypeList(TransportSearchDTO dto) {
        return transportMapper.selectCargoTypeList(dto);
    }

    @Override
    public List<ClientCodeVO> selectClientCodeList(TransportSearchDTO dto) {
        return transportMapper.selectClientCodeList(dto);
    }
}
